package com.lins.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.List;

@Slf4j
public class ObjectCheck {

//    public static final Logger logger = LoggerFactory.getLogger(ObjectCheck.class);

    /**
     * 所有参数不允许为空，否则返回false
     **/
    public static boolean validateProperty(Object object){
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();

        for (Field f : fields) {
            //属性被修饰，不能访问，设置可到达，性能有了20倍的提升，功能是启用或禁用安全检查
            f.setAccessible(true);
            try {
                Object o = f.get(object);
                if (StringUtils.isEmpty((CharSequence) o)) return false;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     *
     * @Title: validateFild
     * @Description: 检查指定对象的属性值是否为空
     * @param: @param object
     * @param: @param exclFild
     * @param: @return
     * @return: boolean
     * @throws
     */
    public static boolean validateFild(Object object){
        //默认验证不通过
        boolean target = false;
        //遍历所有属性
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            try {
//                String name = f.getName();
                Object o = f.get(object);
                //判断字段是否为空，对象属性中的属性都会转为对象类型来判断
                if (null != o) {
                    target = true;
                }
            } catch (IllegalArgumentException e) {
                target = false;
                log.error("对象属性解析异常" + e.getMessage());
                return target;
//                throw new RuntimeException("对象属性解析异常" + e.getMessage());
            } catch (IllegalAccessException e) {
                target = false;
                log.error("对象属性解析异常" + e.getMessage());
                return target;
//                throw new RuntimeException("对象属性解析异常" + e.getMessage());
            }
        }

        return target;
    }
    /**
     *
     * @Title: validateFild
     * @Description: 检查指定对象的属性值是否为空，排除指定的属性值
     * @param: @param object
     * @param: @param exclFild
     * @param: @return
     * @return: boolean
     * @throws
     */
    public static boolean validateFild(Object object, List exclFild){
        boolean target = false;
        for (Field f : object.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            try {
                String name = f.getName();
                // 判断属性名称是否在排除属性值中
                if(!exclFild.contains(name)){
                    if (f.get(object) != null) { //判断字段是否为空，并且对象属性中的基本都会转为对象类型来判断
                        target = true;
                        break;
                    }
                }
            } catch (IllegalArgumentException e) {
                target = false;
                log.error("对象属性解析异常" + e.getMessage());
                return target;
//                throw new RuntimeException("对象属性解析异常" + e.getMessage());
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                target = false;
                log.error("对象属性解析异常" + e.getMessage());
                return target;
//                throw new RuntimeException("对象属性解析异常" + e.getMessage());
            }
        }

        return target;
    }

}